<template>
  <div class="app-container">
    <el-form v-model="demo">
      <el-row>
        <el-col :span="8">
        <el-form-item prop="displayName" label="名字" label-width="80px">
          <el-input v-model="demo.displayName" icon="search" name="title" placeholder="请输入名字"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item prop="name" label="手机号" label-width="80px">
          <el-input v-model="demo.cellphone" @blur="cellphoneTest" icon="search" name="title" type="number" placeholder="请输入手机号"></el-input>
        </el-form-item>
      </el-col>
      </el-row>
      <el-form-item prop="name" label="权限" label-width="80px" style="width: 200px;">
         <el-select v-model="demo.value" placeholder="请选择" style="width: 160px;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
        <el-row>
          <el-col :span="8">
          <el-form-item prop="name" label="后台账号" label-width="80px">
            <el-input v-model="demo.name" icon="search" name="title" placeholder="XXX@zhonglongedu.com"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="password" label="密码" label-width="80px">
            <el-input v-model="demo.password" icon="search" name="title" placeholder="123456" value="123456"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
       <el-button type="primary" @click="sendOne" style="margin-top: 8px;margin-left: 20px;">确定</el-button>
        <el-button type="primary" @click="sendNext" style="margin-top: 8px;margin-left: 20px;"></i>添加下一个</el-button>
  </div>
</template>

<script>
import { fetchList } from "@/api/article";
import axios from "axios";
import api from "@/api/configure";
import { Message } from "element-ui";
export default {
  name: "InlineEditTable",
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      options: [
        {
          value: "ADMIN",
          label: "管理员"
        },
        {
          value: "OPERATION",
          label: "普通"
        }
      ],
      value: "",
      demo: {
        name: "",
        packageName: "",
        displayName: "",
        cellphone: "",
        password: "123456"
      },
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      fileData: []
    };
  },
  created() {},
  methods: {
    cellphoneTest() {
      let cellphoneData = this.demo.cellphone;
      if (cellphoneData.length != 11) {
        this.$message({
          showClose: true,
          message: "手机号必须11位,请重新输入!",
          type: "error"
        });
      }
    },
    sendNext() {
      let cellphoneLength = this.demo.cellphone;
      if (cellphoneLength.length == 11) {
        this.Rest();
      } else {
        this.$message({
          showClose: true,
          message: "手机号必须11位,请重新输入!",
          type: "error"
        });
        return false;
      }
    },
    sendOne() {
      let cellphoneLength = this.demo.cellphone;
      if (cellphoneLength.length == 11) {
        this.sureImport();
      } else {
        this.$message({
          showClose: true,
          message: "手机号必须11位,请重新输入!",
          type: "error"
        });
        return false;
      }
    },
    Rest() {
      let data = {
        username: this.demo.name,
        displayName: this.demo.displayName,
        password: this.demo.password,
        cellphone: this.demo.cellphone,
        role: {
          code: this.demo.value
        }
      };
      api.userAdd(data).then(({ success, data }) => {
        this.$message({
          showClose: true,
          message: "添加成功！",
          type: "success"
        });
        this.demo.name = "";
        this.demo.cellphone = "";
        this.demo.value = "";
        this.demo.displayName = "";
        this.demo.password = "";
      });
    },
    sureImport() {
      let data = {
        username: this.demo.name,
        displayName: this.demo.displayName,
        password: this.demo.password,
        cellphone: this.demo.cellphone,
        role: {
          code: this.demo.value
        }
      };
      api.userAdd(data).then(response => {
        if (response.status != 200) {
          this.$message({
            showClose: true,
            message: "添加成功！",
            type: "success"
          });
          this.$router.push("/configure");
        } else {
          this.$message({
            showClose: true,
            message: response.message,
            type: "error"
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
#upClass {
  width: 420px;
  height: 35px;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
